<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
// use App\Http\Requests\UserInsertRequest;
use App\User;
use Hash;
use DB;
class UserController extends Controller
{
    public function login()
    {
        // echo 124;
        return view('qian.index');
    }

    public function denglu(Request $request)
    {
        // echo 1243;
        //获取判断手机号码
        // if(empty())
         $email = $request->email;
         // var_dump($phone);
          //读取数据库
          
        $user = DB::table('users')->where('email',$email)->first();

        if(!empty($user)){
           //  // var_dump($_COOKIE);die;
           //  $user = DB::table('users')->get();
           //  // DB::table('users')->first()
            // dd($user);die;
            $emails = $user->email;
           // var_dump($emails);
           //  //获取 判断密码
            $password = $request->password;
            // dd($password);
             $user = DB::table('users')->where('password',$password)->first();
            // var_dump($user);die;
             //读取数据库
            $passwords = $user->password;
            // var_dump($passwords);die;
            // var_dump($_COOKIE);
            if(($email == $emails) && ($password == $passwords)){
                echo '登录成功';
            }else{
                echo '登录失败';
            }
        }else{
            return view('qian.zhuce');
        }
    }

    //zhu注册显示页面
    public function zhuce()
    {
        return view('qian.zhuce');
    }
    
    // 入库
    public function dozhuce(Request $request)
    {
        // echo 2345;
       // if(!$user = new User;){

       // }
         //获取数据 入库
        $this->validate($request, [
                'username' => 'required|regex:/^\w{6,20}$/',
                'password' => 'required|regex:/^\S{6,20}$/|same:repassword',
                'email' =>'required|email',
                // 'phone'=>'request|regex:/^1\d{10}$/'
            ]);
            // [
            //     'username.required'=>'对不起用户名必填',
            //     'username.regex'=>'用户名格式不正确',
            //     'username.unique'=>'用户名已经存在',
            //     'password.required' => '密码必填',
            //     'password.regex' => '密码格式不正确',
            //     'password.same' => '两次密码不一致',
            //     'email.email'=>'邮箱格式不正确'
            // ]);
           

       // $res = $request -> all();
       // var_dump($res);
        $user = new User;
        // var_dump($user);
       $res =  $user -> username = $request->username;
       // dd($res);die;
        $user -> nikename = $request->nikename;
        $user -> password = $request->password;//Hash::make($request->input('password'));加密判断
        $user -> email = $request -> email;  
        $user -> phone = $request -> phone;  






        //写入
        // $user->save();
        if($user->save()) {
            return redirect('/login');
        }else{
            return back();
        }
        
        
    }
}
